package org.seedltd.upms.service.impl.detection;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.seedltd.core.base.BasePage;
import org.seedltd.core.base.BaseServiceImpl;
import org.seedltd.core.utils.Assert;
import org.seedltd.core.utils.BeanUtils;
import org.seedltd.core.utils.StringUtils;
import org.seedltd.core.mybatis.conditions.ProQuery;
import org.seedltd.upms.enums.EnableStatus;
import org.seedltd.upms.mapper.detection.DetectionProductItemMapper;
import org.seedltd.upms.model.detection.entity.DetectionProductItem;
import org.seedltd.upms.model.detection.form.DetectionProductItemForm;
import org.seedltd.upms.model.detection.form.DetectionProductItemSearchForm;
import org.seedltd.upms.model.detection.vo.DetectionProductItemVO;
import org.seedltd.upms.service.detection.DetectionProductItemService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 产品检测项业务实现
 * 
 * @author laizuan
 * @since 2021/08/25
 */
@Service
@AllArgsConstructor
public class DetectionProductItemServiceImpl extends BaseServiceImpl<DetectionProductItemMapper, DetectionProductItem>
    implements DetectionProductItemService {

    @Override
    public DetectionProductItemVO findById(Long id) {
        DetectionProductItem pojo = baseMapper.selectById(id);
        if (pojo == null) {
            return null;
        }
        DetectionProductItemVO resultVo = new DetectionProductItemVO();
        BeanUtils.copyProperties(pojo, resultVo);
        return resultVo;
    }

    @Override
    public Long add(DetectionProductItemForm form) {
        DetectionProductItem pojo = new DetectionProductItem();
        BeanUtils.copyProperties(form, pojo);
        baseMapper.insert(pojo);
        return pojo.getId();
    }

    @Override
    public boolean update(DetectionProductItemForm form) {
        DetectionProductItem pojo = baseMapper.selectById(form.getId());
        Assert.notNull(pojo, "未找到需要修改的数据");
        BeanUtils.copyProperties(form, pojo);
        return baseMapper.updateById(pojo) > 0;
    }

    @Override
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public BasePage<DetectionProductItemVO> page(DetectionProductItemSearchForm searchForm) {
        return ProQuery.Builder(baseMapper).searchForm(searchForm).build()
            .selectPage(BasePage.of(searchForm.getStart(), searchForm.getSize()), DetectionProductItemVO.class);
    }

    @Override
    public List<Map<String, Object>> listAll() {
        return   ProQuery.Builder(baseMapper).build().select(DetectionProductItem::getItemName)
                .eq(DetectionProductItem::getEnabled, EnableStatus.ENABLE.getValue()).selectMaps();
    }

    @Override
    public List<DetectionProductItemVO> listAll2() {
        List<DetectionProductItem> list = ProQuery.Builder(baseMapper).build().select(DetectionProductItem::getItemName,
                DetectionProductItem::getItemValues, DetectionProductItem::getScope)
            .eq(DetectionProductItem::getEnabled, EnableStatus.ENABLE.getValue()).selectList();
        List<DetectionProductItemVO> result = Lists.newArrayListWithCapacity(list.size());
        DetectionProductItemVO vo;
        for (DetectionProductItem detectionProductItem : list) {
            vo = new DetectionProductItemVO();
            vo.setScope(detectionProductItem.getScope());
            vo.setItemValueList(Arrays.asList(StringUtils.split(detectionProductItem.getItemValues(), ",")));
            vo.setItemName(detectionProductItem.getItemName());
            result.add(vo);
        }
        return result;
    }
}